/*
 * Copyright (c) 2019-2020, https://api.iot.10086.cn/(https://gitee.com/cmcc-iot-api/iot-card-open-application/).
 */
package cn.iot.card.open.sim.controller;

import cn.iot.card.open.context.LoginContextHolder;
import cn.iot.card.open.context.model.LoginUser;
import cn.iot.card.open.exception.enums.BizExceptionEnum;
import cn.iot.card.open.modular.entity.AccountCard;
import cn.iot.card.open.modular.entity.Card;
import cn.iot.card.open.modular.entity.UserGroupRl;
import cn.iot.card.open.modular.log.ApiBussinessLog;
import cn.iot.card.open.modular.service.AccountCardService;
import cn.iot.card.open.modular.service.CardService;
import cn.iot.card.open.modular.service.UserService;
import cn.iot.card.open.pojo.page.LayuiPageFactory;
import cn.iot.card.open.pojo.page.LayuiPageInfo;
import cn.iot.card.open.sim.dto.siminfo.CardBindingDTO;
import cn.iot.card.open.sim.dto.siminfo.SimApnGprsAlarmDTO;
import cn.iot.card.open.sim.dto.siminfo.SimRealNameRegisterDTO;
import cn.iot.card.open.sim.service.SimInfoForSecurityService;
import cn.iot.card.open.util.CommonUtil;
import cn.stylefeng.roses.core.reqres.response.ResponseData;
import cn.stylefeng.roses.kernel.model.exception.ServiceException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

/**
 * 物联卡综合信息--安全
 *
 * @author zhanlele
 * @date 20200326 15:14:28
 * @modify 20200326 zhanlele v1.1.2 创建
 * @since v1.1.2
 */
@Controller
@RequestMapping("/security")
public class SimInfoForSecurityController {

    /**
     * 页面路径地址前缀
     */
    private static String PREFIX = "/modular/sim/general/security/";

    @Autowired
    private CardService cardService;

    @Autowired
    private UserService userService;

    @Autowired
    private AccountCardService accountCardService;

    @Autowired
    private SimInfoForSecurityService simInfoForSecurityService;

    /**
     * 物联卡机卡分离状态
     *
     * @param msisdn   物联卡msisdn号
     * @param testType 分离检测方式
     * @return 返回 layui page info
     * @author zhanlele
     * @date 20200326 15:37:03
     * @modify 20200326 zhanlele v1.1.2 创建
     * @since v1.1.2
     */
    @RequestMapping("/machineCardSeparateStatus")
    @ResponseBody
    public LayuiPageInfo machineCardSeparateStatus(String msisdn, String testType) throws Exception {
        Card card = cardService.verifyCardInfo(msisdn, null, null);
        if (null == card) {
            return LayuiPageFactory.createBizExceptionMsg(BizExceptionEnum.CARD_NOT_EXIT_IN_SYSTEM);
        }

        LoginUser loginUser = LoginContextHolder.getContext().getUser();
        UserGroupRl userGroupRl = userService.getUserGroupRlByUserAndCard(loginUser.getId(), card.getCardId());
        if (null == userGroupRl) {
            return LayuiPageFactory.createBizExceptionMsg(BizExceptionEnum.CARD_NOT_BELONG_USER);
        }

        AccountCard accountCard = accountCardService.getBaseMapper().findByCardId(card.getCardId());
        String accountId = String.valueOf(accountCard.getAccountId());
        String transId = CommonUtil.generateTransId(accountId);

        return simInfoForSecurityService.machineCardSeparateStatus(accountId, transId, msisdn, testType);
    }

    /**
     * 跳转到机卡绑定页面
     *
     * @return 返回 string
     * @author zhanlele
     * @date 20200326 15:58:50
     * @modify 20200326 zhanlele v1.1.2 创建
     * @since v1.1.2
     */
    @RequestMapping("/toMachineCardBind")
    public String toMachineCardBind() {
        return PREFIX + "machineCardBind.html";
    }

    /**
     * 机卡绑定（话单侧、网络侧）
     *
     * @param cardBindingDTO
     * @return 返回 response data
     * @throws Exception
     * @author zhanlele
     * @date 20200326 22:40:22
     * @modify 20200326 zhanlele v1.1.2 创建
     * @since v1.1.2
     */
    @RequestMapping(value = {"/cardBindForCdr", "/cardBindForWeb"})
    @ApiBussinessLog(value = "机卡绑定操作")
    @ResponseBody
    public ResponseData cardBind(CardBindingDTO cardBindingDTO) throws Exception {
        String cardType = cardBindingDTO.getCardType();
        String cardNum = cardBindingDTO.getCardNum();
        Card card = cardService.findCard(cardType, cardNum);
        if (null == card) {
            throw new ServiceException(BizExceptionEnum.CARD_NOT_EXIT_IN_SYSTEM);
        }

        LoginUser loginUser = LoginContextHolder.getContext().getUser();
        UserGroupRl userGroupRl = userService.getUserGroupRlByUserAndCard(loginUser.getId(), card.getCardId());
        if (null == userGroupRl) {
            throw new ServiceException(BizExceptionEnum.CARD_NOT_BELONG_USER);
        }

        AccountCard accountCard = accountCardService.getBaseMapper().findByCardId(card.getCardId());
        String accountId = String.valueOf(accountCard.getAccountId());
        String transId = CommonUtil.generateTransId(accountId);

        return simInfoForSecurityService.cardBind(accountId, transId, cardBindingDTO);
    }

    /**
     * 跳转物联卡实名认证页面
     *
     * @return 返回 string
     * @author zhanlele
     * @date 20200327 10:18:29
     * @modify 20200327 zhanlele v1.1.2 创建
     * @since v1.1.2
     */
    @RequestMapping("/toSimRegister")
    public String toSimRegister() {
        return PREFIX + "simRegister.html";
    }

    /**
     * 物联卡实名登记
     *
     * @param userPic                用户现场人像图片
     * @param idCardPicFace          用户身份证芯片头像图片
     * @param idCardPicFront         用户身份证正面图片
     * @param idCardPicBack          用户身份证反面图片
     * @param simRealNameRegisterDTO
     * @return 返回 response data
     * @throws Exception
     * @author zhanlele
     * @date 20200329 11:17:05
     * @modify 20200329 zhanlele v1.1.2 创建
     * @since v1.1.2
     */
    @RequestMapping(value = "/simRealNameRegister", method = RequestMethod.POST)
    @ApiBussinessLog(value = "物联卡实名登记")
    @ResponseBody
    public ResponseData simRealNameRegister(MultipartFile userPic, MultipartFile idCardPicFace, MultipartFile idCardPicFront,
                                            MultipartFile idCardPicBack, SimRealNameRegisterDTO simRealNameRegisterDTO) throws Exception {
        String msisdn = simRealNameRegisterDTO.getMsisdn();
        String iccid = simRealNameRegisterDTO.getIccid();
        Card card = cardService.verifyCardInfo(msisdn, iccid, null);
        if (null == card) {
            throw new ServiceException(BizExceptionEnum.CARD_NOT_EXIT_IN_SYSTEM);
        }

        LoginUser loginUser = LoginContextHolder.getContext().getUser();
        UserGroupRl userGroupRl = userService.getUserGroupRlByUserAndCard(loginUser.getId(), card.getCardId());
        if (null == userGroupRl) {
            throw new ServiceException(BizExceptionEnum.CARD_NOT_BELONG_USER);
        }

        AccountCard accountCard = accountCardService.getBaseMapper().findByCardId(card.getCardId());
        String accountId = String.valueOf(accountCard.getAccountId());
        String transId = CommonUtil.generateTransId(accountId);

        return simInfoForSecurityService.simRealNameRegister(accountId, transId, userPic, idCardPicFace, idCardPicFront,
                idCardPicBack, simRealNameRegisterDTO);
    }

    /**
     * 跳转物联卡流量告警页面
     *
     * @return 返回 string
     * @author zhanlele
     * @date 20200329 11:25:25
     * @modify 20200329 zhanlele v1.1.2 创建
     * @since v1.1.2
     */
    @RequestMapping("/toApnGprsAlarm")
    public String toApnGprsAlarm() {
        return PREFIX + "simApnGprsAlarm.html";
    }

    /**
     * 物联卡APN流量告警规则设置
     *
     * @param simApnGprsAlarmDTO
     * @return 返回 response data
     * @throws Exception
     * @author zhanlele
     * @date 20200329 18:25:10
     * @modify 20200329 zhanlele v1.1.2 创建
     * @since v1.1.2
     */
    @RequestMapping("/simApnGprsAlarm")
    @ApiBussinessLog(value = "物联卡APN流量告警规则设置")
    @ResponseBody
    public ResponseData simApnGprsAlarm(SimApnGprsAlarmDTO simApnGprsAlarmDTO) throws Exception {
        Card card = cardService.verifyCardInfo(simApnGprsAlarmDTO.getMsisdns(), null, null);
        if (null == card) {
            throw new ServiceException(BizExceptionEnum.CARD_NOT_EXIT_IN_SYSTEM);
        }

        LoginUser loginUser = LoginContextHolder.getContext().getUser();
        UserGroupRl userGroupRl = userService.getUserGroupRlByUserAndCard(loginUser.getId(), card.getCardId());
        if (null == userGroupRl) {
            throw new ServiceException(BizExceptionEnum.CARD_NOT_BELONG_USER);
        }

        AccountCard accountCard = accountCardService.getBaseMapper().findByCardId(card.getCardId());
        String accountId = String.valueOf(accountCard.getAccountId());
        String transId = CommonUtil.generateTransId(accountId);

        return simInfoForSecurityService.simApnGprsAlarm(accountId, transId, simApnGprsAlarmDTO);
    }
}
